IMAGE_VERSION = "1.0"
VERSION       = File.read("../VERSION.txt").strip
RUBY_VERSIONS = File.read("../RUBY_VERSIONS.txt").strip.split(/\s+/)
ARCHITECTURES = ["x86_64"]
CONCURRENCY   = `./internal/cpucount`.to_i

task :default => :package

desc "Create packages for all Ruby versions (#{RUBY_VERSIONS.join(' ')}) and all architectures (#{ARCHITECTURES.join(' ')}"
task :package do
  # Do nothing
end

desc "Build the Docker images"
task :image do
  # Do nothing
end

desc "Build binaries for all Ruby versions (#{RUBY_VERSIONS.join(' ')}) and all architectures (#{ARCHITECTURES.join(' ')}"
task :build do
  # Do nothing
end

desc "Test all Ruby versions (#{RUBY_VERSIONS.join(' ')})"
task :test do
  # Do nothing
end

desc "Upload all packages to the server"
task :upload do
  # Do nothing
end

desc "Clean all packages"
task :clean do
  sh "rm -rf output"
end

ARCHITECTURES.each do |arch|
  task :image => "image:#{arch}"

  desc "Build the #{arch} Docker image"
  task "image:#{arch}" do
    sh "docker build --pull" \
      " -t phusion/traveling-ruby-builder-#{arch}:#{IMAGE_VERSION}" \
      " -f image/Dockerfile-#{arch} image"
  end

  desc "Open a shell for testing building the #{arch} Docker image"
  task "image:buildtest:#{arch}" do
    File.read("image/Dockerfile-#{arch}") =~ /FROM (.+)/
    base_image = $1
    sh "docker", "run", "-ti", "--rm",
      "-v", File.absolute_path("image") + ":/tr_build:ro",
      "-v", File.absolute_path("image") + ":/host:delegated",
      base_image, "bash"
  end


  RUBY_VERSIONS.each do |ruby_version|
    package = "traveling-ruby-#{VERSION}-#{ruby_version}-linux-#{arch}.tar.gz"
    gem_dir = "traveling-ruby-gems-#{VERSION}-#{ruby_version}-linux-#{arch}"

    task :package => "package:#{ruby_version}:#{arch}"
    task :build   => "build:#{ruby_version}:#{arch}"
    task :test    => "test:#{ruby_version}:#{arch}"
    task :upload  => "upload:#{ruby_version}:#{arch}"
    task :clean   => "clean:#{ruby_version}:#{arch}"

    desc "Create packages for Ruby #{ruby_version} #{arch}"
    task "package:#{ruby_version}:#{arch}" => [package, "#{gem_dir}/ok"]

    desc "Build binaries for Ruby #{ruby_version} #{arch}"
    task "build:#{ruby_version}:#{arch}" => "output/#{ruby_version}-#{arch}/bin"


    file(package => "output/#{ruby_version}-#{arch}/bin") do
      sh "./package -r #{package} output/#{ruby_version}-#{arch}"
    end

    file("#{gem_dir}/ok" => "output/#{ruby_version}-#{arch}/bin") do
      sh "./package -E #{gem_dir} output/#{ruby_version}-#{arch}"
      touch "#{gem_dir}/ok"
    end

    # We use 'file' instead of 'directory' here so that packages are updated
    # whenever we update binaries.
    file("output/#{ruby_version}-#{arch}/bin" => "cache/#{ruby_version}-#{arch}") do
      sh "mkdir -p output/#{ruby_version}-#{arch}"
      sh "./build-ruby -j #{CONCURRENCY} -i phusion/traveling-ruby-builder-#{arch}:#{IMAGE_VERSION}" \
        " -r #{ruby_version} -c cache/#{ruby_version}-#{arch}" \
        " output/#{ruby_version}-#{arch}"
    end

    directory("cache/#{ruby_version}-#{arch}")


    desc "Test Ruby #{ruby_version} #{arch}"
    task "test:#{ruby_version}:#{arch}" => "output/#{ruby_version}-#{arch}/bin" do
      sh "./test-gems output/#{ruby_version}-#{arch}"
    end

    desc "Upload Ruby #{ruby_version} #{arch} packages to the server"
    task "upload:#{ruby_version}:#{arch}" => [package, "#{gem_dir}/ok"] do
      sh "env IMAGE_VERSION=#{IMAGE_VERSION} ./upload #{package} #{gem_dir}"
    end

    desc "Clean Ruby #{ruby_version} packages"
    task "clean:#{ruby_version}:#{arch}" do
      sh "rm -rf #{package} #{gem_dir} output/#{arch}"
    end
  end
end
